package com.jair.jairaiagent.rag;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 读取Markdown知识库
 */
@Slf4j
@Component
public class LoveAppDocumentLoder {

    private final ResourcePatternResolver resourcePatternResolver;

    public LoveAppDocumentLoder(ResourcePatternResolver resourcePatternResolver) {
        this.resourcePatternResolver = resourcePatternResolver;
    }

    public List<Document> loadMarkDowns(){
        //所有文档的切片
        List<Document> allDocuments = new ArrayList<>();
        try {
            //读取文件
            Resource[] resources = resourcePatternResolver.getResources("classpath:/ragKnowledge/*.md");
            //配置读取markdown文件的设定
            for (Resource resource : resources) {
                String fileName = resource.getFilename();
                //读取文件
                MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                        .withHorizontalRuleCreateDocument(true)  // 是否将水平规则作为文档
                        .withIncludeCodeBlock(false)   // 是否将代码块作为文档
                        .withIncludeBlockquote(false)   // 是否将引用作为文档
                        .withAdditionalMetadata("filename", fileName)  // 将文件名作为元数据
                        .build();  // 创建MarkdownDocumentReader实例
                MarkdownDocumentReader reader = new MarkdownDocumentReader(resource, config);
                allDocuments.addAll(reader.get());  // 获取文档
            }
        } catch (Exception e) {
            log.error("读取Markdowns失败: {}", e.getMessage());
        }
        return allDocuments;
    }
}

